package work.xkong.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import work.xkong.common.Const;
import work.xkong.vo.outputvo.LoginInfoOutputVo;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @PACKAGE_NAME: work.xkong.utils
 * @NAME: TokenUtils
 * @USER: NuanYang
 * @author: 查炼飞
 * @create: 2022/11/22 20:16
 */
public class TokenUtils {

    public static String getToken(LoginInfoOutputVo loginInfoOutputVo){
        long startTime = System.currentTimeMillis();
        String token = Jwts.builder()
                .claim("type",loginInfoOutputVo.getType())
                .setId(loginInfoOutputVo.getUserId().toString())
                .setSubject(loginInfoOutputVo.getUsername())
                .signWith(SignatureAlgorithm.HS256, Const.SIGNATURE_KEY)
                .setIssuedAt(new Date(startTime))
                .setExpiration(new Date(startTime + Const.HAFT_HOUR))
                .compact();

        return token;
    }

    public static Claims getParseToken(String jwt){
        return (Claims) Jwts.parser()
                .setSigningKey(Const.SIGNATURE_KEY)
                .parse(jwt)
                .getBody();
    }
}
